#!/usr/bin/env bash
. ./wvtest-bup.sh || exit $?
. ./dev/lib.sh || exit $?

set -o pipefail

top="$(WVPASS pwd)" || exit $?
tmpdir="$(WVPASS wvmktempdir)" || exit $?

bup() { "$top/bup" "$@"; }

WVPASS cd "$tmpdir"

WVSTART 'command without init fails'
WVPASS mkdir foo
bup -d nope save -t foo
rc=$?
WVPASSEQ 2 "$rc"
WVPASS rmdir foo

WVSTART '-d repo argument'
WVPASS test ! -e repo
WVPASS bup -d repo init
WVPASS test -d repo/refs/heads
WVPASS test -d repo/objects/pack
WVPASS rm -rf repo

WVSTART 'positional repo argument'
WVPASS test ! -e repo
WVPASS bup init repo
WVPASS test -d repo/refs/heads
WVPASS test -d repo/objects/pack
WVPASS rm -rf repo

WVSTART 'positional repo argument precdence over -d'
WVPASS test ! -e repod
WVPASS test ! -e repo
WVPASS bup -d repod init repo
WVPASS test ! -e repod
WVPASS test -d repo/refs/heads
WVPASS test -d repo/objects/pack
WVPASS rm -rf repo

WVSTART 'positional repo argument precdence over BUP_DIR'
WVPASS test ! -e repod
WVPASS test ! -e repo
export BUP_DIR=repod
WVPASS bup init repo
unset BUP_DIR
WVPASS test ! -e repod
WVPASS test -d repo/refs/heads
WVPASS test -d repo/objects/pack
WVPASS rm -rf repo

bup init /dev/null || rc=$?
WVPASSEQ "$rc" "$bup_exit_failure"

WVPASS cd "$top"
WVPASS rm -rf "$tmpdir"
